<template>
    <!-- 消费凭密功能设置 -->
    <div class="consumptionBysecret" v-loading="isLoading">
        <cardComponent pageTitle="消费凭密功能设置">
            <template slot="cardBody">
                <div class="dialogVisible">
                    <el-form :model="searchFormOne" size="small" label-width="50%">
                        <!-- <el-row>
                        <el-col :span="8">-->
                        <el-form-item label="账户类别：" label-position="left" prop="mainCardNo">
                            <el-input disabled v-model="accountType" style="width: 200px;"></el-input>
                        </el-form-item>
                        <!-- </el-col>
                        <el-col :span="12">-->
                        <el-form-item label="卡号：" label-position="left" prop="CARDNO">
                            <el-input v-model="searchFormOne.CARDNO" disabled></el-input>
                        </el-form-item>
                        <!-- </el-col>
                        </el-row>
                        <el-col :span="8">-->
                        <el-form-item label="消费是否使用密码：" label-position="left" prop="openOrNoPass">
                            <span>{{ this.PINCHK === 0 ? '不使用密码' : '使用密码'}}</span>
                        </el-form-item>
                        <!-- </el-col>
                        <el-col :span="12">-->
                        <div class="btnBox">
                            <el-button
                                type="primary"
                                @click="hanleSure"
                                v-if="this.PINCHK === 1"
                            >关闭凭密</el-button>
                            <el-button
                                type="primary"
                                @click="hanleSure"
                                v-if="this.PINCHK === 0"
                            >开通凭密</el-button>
                        </div>
                        <!-- </el-col>                             -->
                    </el-form>
                </div>
            </template>
        </cardComponent>
    </div>
</template>

<script>
import { mapState } from 'vuex';
import { reolaceString, getAccountTypeName } from '@/utils/Common';
import cardComponent from '@/components/cardComponent/cardComponent.vue';

export default {
    name: 'consumptionBysecret',
    components: {
        cardComponent,
    },
    data() {
        return {
            searchFormOne: {
                CARDNO: '',
            },
            // 是否使用密码
            PINCHK: '',
            // 是否开通
            SERCNT: '99999999999999999999',
            isLoading: false,
            accountType: '',
        };
    },
    filters: {

    },
    async mounted() {
        this.getCardInfo();
        this.searchFormOne.CARDNO = this.cardInfo_3004.CARDNO;
        this.accountType = await getAccountTypeName(this.cardList_3601, this.cardInfo_3004, this);
    },
    computed: mapState({
        cardInfo_3004: state => state.common.cardInfo_3004,
        cardList_3601: state => state.common.cardList_3601,
        defaultParams: state => state.common.defaultParams,
        defaultpinFlag: state => state.common.pinFlag,
    }),
    methods: {
        // 点击按钮进行是否
        hanleSure() {
            this.hanleTableData();
        },
        // 进入页面获取卡信息
        getCardInfo() {
            this.isLoading = true;
            const appBody = {
                CARDNBR: this.cardInfo_3004.CARDNO || '', // 卡号
                OPT: '1', // 查询方式 1-卡号查询  2-证件号查询
                PINFLAG: this.defaultpinFlag, // 是否检查密码标志
                PIN: '123456', // 密码
            };
            const defaultParams = JSON.parse(JSON.stringify(this.defaultParams));
            defaultParams.body.appHead.tranCode = '3014';
            defaultParams.body.appBody = appBody;
            // 3014交易
            this.$post.common.transPort(defaultParams).then((res) => {
                this.isLoading = false;
                if (res.status === 200) {
                    const result = JSON.parse(res.data.jsonRes);
                    // console.log(result.body.appBody.PINCHK, 111111)
                    if (result.sysHead.rspCd === '000000') {
                        this.PINCHK = result.body.appBody.PINCHK;
                    } else {
                        const errorMsg = `错误代码：${result.sysHead.rspCd}, 错误信息：${result.sysHead.rspMsg}`;
                        this.$alert(errorMsg, '提示', {
                            customClass: 'error', type: 'error',
                        });
                    }
                } else {
                    this.$alert('请求失败！', '提示', {
                        customClass: 'error', type: 'error',
                    });
                }
            });
        },
        hanleTableData() {
            if (this.PINCHK === 0) {
                this.SERCNT = reolaceString(this.SERCNT, 0, 1);
            } else if (this.PINCHK === 1) {
                this.SERCNT = reolaceString(this.SERCNT, 0, 0);
            }
            const appBody = {
                CARDNBR: this.cardInfo_3004.CARDNO || '', // 卡号
                PINFLAG: this.defaultpinFlag, // 是否检查密码标志
                SERCNT: this.SERCNT, // 0为不开通 1为开通 9为不作修改
                STSCHK: 'N', // N：不检查，任何状态都允许操作 其他：检查 卡片检查状态
            };
            const defaultParams = JSON.parse(JSON.stringify(this.defaultParams));
            defaultParams.body.appHead.tranCode = '4012';
            defaultParams.body.appBody = appBody;
            // 4012交易
            this.$confirm('确定修改？', '提示', { customClass: 'warning', type: 'warning' }).then(() => {
                this.isLoading = true;
                this.$post.common.transPort(defaultParams).then((res) => {
                    this.isLoading = false;
                    if (res.status === 200) {
                        const result = JSON.parse(res.data.jsonRes);
                        // console.log(result, 111111)
                        if (result.sysHead.rspCd === '000000') {
                            this.searchForm = result.body.appBody || [];
                            this.$alert(result.sysHead.rspMsg, '提示', {
                                customClass: 'success', type: 'success',
                            });
                            this.getCardInfo();
                        } else {
                            const errorMsg = `错误代码：${result.sysHead.rspCd}, 错误信息：${result.sysHead.rspMsg}`;
                            this.$alert(errorMsg, '提示', {
                                customClass: 'error', type: 'error',
                            });
                        }
                    } else {
                        this.$alert('请求失败！', '提示', {
                            customClass: 'error', type: 'error',
                        });
                    }
                });
            }).catch(() => {

            });
        },

    },
};
</script>

<style lang='scss'>
.consumptionBysecret {
    width: 98%;
    background: #fff;
    // padding:10px;
    box-sizing: border-box;
    border-radius: 5px;

    /* //头部内容 */
    .tab-header {
        width: 100%;
        text-align: left;
        span {
            font-size: 14px;
        }
        .headerTitle {
            font-size: 16px;
            margin: 10px 0px;
            color: skyblue;
        }
    }
    .dialogVisible {
        margin: 30px auto 0;
        .el-form-item__content {
            text-align: left;
        }
    }
    .btnBox {
        text-align: center;
    }
}
</style>
